package com.zmn.plat.services.impl.channel.sp;

import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.manager.common.database.annotation.ReadOnlyConnection;
import com.zmn.plat.model.entity.channel.sp.ChannelSp;
import com.zmn.plat.model.entity.channel.sp.ChannelSpQuery;
import com.zmn.plat.persistence.interfaces.channel.sp.ChannelSpDao;
import com.zmn.plat.services.interfaces.channel.sp.ChannelSpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;


/**
 * 描述: 渠道服务商接口实现类
 *
 * @author jiafei
 * @since 2021-06-09 16:50:59
 */
@Slf4j
@Service
public class ChannelSpServiceImpl implements ChannelSpService {

    private static final String TAG = "渠道服务商基础服务接口";

    @Resource
    private ChannelSpDao channelSpDao;

    @Override
    public ChannelSp findById(Integer id) {

        if (Objects.isNull(id)) {
            log.error("[{}],根据id查询数据id为null", TAG);
            return null;
        }
        return channelSpDao.findById(id);
    }

    @Override
    @ReadOnlyConnection
    public List<ChannelSp> listPageByQuery(ChannelSpQuery query) {

        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return null;
        }
        query.setTotalCount(channelSpDao.countByQuery(query));
        return channelSpDao.listPageByQuery(query);
    }

    @Override
    public List<ChannelSp> listByQuery(ChannelSpQuery query) {

        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return null;
        }
        return channelSpDao.listByQuery(query);
    }

    @Override
    public List<ChannelSp> listByChannelIdAndSpIds(Integer channelId, List<Integer> spIds) {

        if (NumberUtil.isNullOrZero(channelId) || CollectionUtil.isNullOrEmpty(spIds)) {
            log.error("[{}],根据渠道id和运营商id查询,channelId:{},spIds:{}", TAG, channelId, spIds);
            return null;
        }
        return channelSpDao.listByChannelIdAndSpIds(channelId, spIds);
    }

    @Override
    public Integer countByQuery(ChannelSpQuery query) {

        if (Objects.isNull(query)) {
            log.error("[{}],按条件统计数据,query为null", TAG);
            return null;
        }
        return channelSpDao.countByQuery(query);
    }

    @Override
    public Integer updateByKey(ChannelSp sp) {

        if (Objects.isNull(sp.getId())) {
            log.error("[{}],根据id修改数据,sp为null", TAG);
            return null;
        }
        return channelSpDao.updateByKey(sp);
    }

    @Override
    public Integer insert(ChannelSp sp) {

        if (Objects.isNull(sp)) {
            log.error("[{}],新增数据,sp为null", TAG);
            return null;
        }
        return channelSpDao.insert(sp);
    }

    @Override
    public void insertBatch(List<ChannelSp> sps) {

        if (CollectionUtil.isNullOrEmpty(sps)) {
            log.error("[{}],批量新增数据,sps为null", TAG);
            return;
        }
        channelSpDao.insertBatch(sps);
    }

    @Override
    public Integer deleteByKey(Integer id) {

        if (Objects.isNull(id)) {
            log.error("[{}],根据id删除数据,id为null", TAG);
            return null;
        }
        return channelSpDao.deleteByKey(id);
    }

    @Override
    public Integer deleteBatchByKeys(List<Integer> ids) {
        if (CollectionUtil.isNullOrEmpty(ids)) {
            log.error("[{}],根据id删除数据,id为null", TAG);
            return null;
        }
        return channelSpDao.deleteBatchByKeys(ids);
    }

    @Override
    public void copy(Integer originalChannelId, int targetChannelId, String operator) {

        channelSpDao.copy(originalChannelId, targetChannelId, operator);
    }

}